package designpatterns.facade.code;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @description: 文件写
 * @author: zk
 * @date: 2023-02-02 17:47
 */
public class FileWriter {

    public void write(String fileNameSrc, String context) {
        System.out.println(String.format("写入文件=>fileNameSrc=%s,context=%s", fileNameSrc,context));
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(fileNameSrc);
            fileOutputStream.write(context.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            System.err.println("文件写入失败！");
        } finally {
            if (Objects.nonNull(fileOutputStream)) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
